package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.FirstRechargeRenewRemindSetting;
import com.iot08.common.entity.app.PxAdverster;
import com.iot08.common.service.base.BaseAdminService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2025/4/25 16:37
 **/
@Slf4j
@Service
public class FirstRechargeRenewRemindService extends BaseAdminService<FirstRechargeRenewRemindSetting> {

    @Resource
    private OperationService operationService;

    /**
     * @description 查询用户首充提醒/续费提醒的配置
     * @author cwj
     * @param [page]
     * @return org.springframework.data.domain.Page<com.iot08.common.entity.app.FirstRechargeRenewRemindSetting>
     * @date @time 2025/4/27 15:53
     **/
    public Page<FirstRechargeRenewRemindSetting> list(PageVO page) {
        PageableImpl pageable = new PageableImpl();
        page.setSort(Sort.by(Sort.Order.desc("createTime")));
        pageable.setPage(page);
        Query query = new Query();
        long count = this.count(query);
        page.setTotalResult((int) count);
        List<FirstRechargeRenewRemindSetting> list = this.find(query.with(pageable));
        Page<FirstRechargeRenewRemindSetting> result = new PageImpl<FirstRechargeRenewRemindSetting>(list, pageable, count);
        return result;
    }

    /**
     * @description 添加充值提醒
     * @author cwj
     * @param [firstRechargeRenewRemindSetting, user, ip]
     * @return void
     * @date @time 2025/4/27 16:41
     **/
    public void add(FirstRechargeRenewRemindSetting firstRechargeRenewRemindSetting, SysUser user, String ip) {
        if (!ObjectUtils.isEmpty(firstRechargeRenewRemindSetting) && ObjectUtils.isEmpty(queryByType(firstRechargeRenewRemindSetting.getType()))) {
            firstRechargeRenewRemindSetting.setCreateTime(new Date());
            firstRechargeRenewRemindSetting.setUpdateTime(new Date());
            this.save(firstRechargeRenewRemindSetting);
            operationService.add(user, ip, "充值提醒配置", "添加",
                    String.format("添加充值提醒配置: %s ", firstRechargeRenewRemindSetting.getTitle()));
        }
    }

    /**
     * @description 查询同类型的充值提醒记录
     * @author cwj
     * @param [type]
     * @return com.iot08.common.entity.app.FirstRechargeRenewRemindSetting
     * @date @time 2025/4/27 16:50
     **/
    public FirstRechargeRenewRemindSetting queryByType(Integer type) {
        return this.findOne(new Query(Criteria.where("type").is(type)));
    }


    /**
     * @description 编辑充值提醒配置
     * @author cwj
     * @param [firstRechargeRenewRemindSetting, user, ip]
     * @return void
     * @date @time 2025/4/29 9:10
     **/
    public void update(FirstRechargeRenewRemindSetting firstRechargeRenewRemindSetting, SysUser user, String ip) {
        FirstRechargeRenewRemindSetting oldFirstRechargeRenewRemindSetting = this.findById(new ObjectId(firstRechargeRenewRemindSetting.getId()));
        FirstRechargeRenewRemindSetting frrrs = queryByType(firstRechargeRenewRemindSetting.getType());
        //APP标识不能重复
        if(!ObjectUtils.isEmpty(oldFirstRechargeRenewRemindSetting)){
            if(ObjectUtils.isEmpty(frrrs) || oldFirstRechargeRenewRemindSetting.getId().equals(frrrs.getId())) {
                Update update = new Update();
                update.set("period",firstRechargeRenewRemindSetting.getPeriod());
                update.set("type",firstRechargeRenewRemindSetting.getType());
                if(!ObjectUtils.isEmpty(firstRechargeRenewRemindSetting.getRecentlyTime())){
                    update.set("recentlyTime",firstRechargeRenewRemindSetting.getRecentlyTime());
                }
                if(!StringUtils.isEmpty(firstRechargeRenewRemindSetting.getTitle())){
                    update.set("title",firstRechargeRenewRemindSetting.getTitle());
                }
                if(!StringUtils.isEmpty(firstRechargeRenewRemindSetting.getContent())){
                    update.set("content",firstRechargeRenewRemindSetting.getContent());
                }
                update.set("updateTime", new Date());
                this.updateById(new ObjectId(firstRechargeRenewRemindSetting.getId()), update);
                operationService.add(user, ip, "充值提醒配置", "编辑",
                        String.format("修改充值提醒配置: %s ", frrrs.getTitle()));
            }
        }
    }
    
}
